package columbia.edu;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Logout
 */
public class Logout extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Logout() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		
		HttpSession session = req.getSession(true);
		session.invalidate();
		System.out.println("Session invalidated");

		//String connectionURL = "jdbc:oracle:thin:@//w4111b.cs.columbia.edu:1521/ADB";
		Connection connection=null;
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		try {
			Class.forName("oracle.jdbc.pool.OracleDataSource");
			/*
			connection = (Connection) req.getSession().getAttribute("Connection");
			if (connection == null) {
				connection = DriverManager.getConnection(connectionURL, "ab3416", "kFxJssQk");
				req.getSession().setAttribute("Connection", connection);
			}*/
			
			//String sql = "delete from login";
			//PreparedStatement pst = connection.prepareStatement(sql);
			//int del=pst.executeUpdate();
			//System.out.println("Number of deleted records: " + del);
			//pst.close();
		}
		catch(ClassNotFoundException e){
			out.println("Couldn't load database driver: " + e.getMessage());
		}
		/*catch(SQLException e){
		out.println("SQLException caught: " + e.getMessage());
		}*/
		catch (Exception e){
		out.println(e);
		}
		finally {
			//req.setAttribute("name", fname.toUpperCase() );
			String nextJSP = "/AuthenticLogin.jsp";
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
			dispatcher.forward(req,res);

			try {
				if (connection != null) connection.close();
			}
			catch (SQLException ignored){
				out.println(ignored);
			}
		}
	
	}

}
